package jam.network.message;

import java.util.*;

//
// =============================================================================
//

public class ServerResponse extends ServerMessage {
    private Message request;
    private List<String> args;
    private String keyword;
    
    //
    // =========================================================================
    //
    
    /**
     * Automatically adds the right true/false for the success.
     * @param success Did the response succeed?
     */
    private void addSuccess(boolean success) {
        if (success)
            this.args.add("TRUE");
        else
            this.args.add("FALSE");
    }
    
    //
    // =========================================================================
    //
    
    @Override
    public String getKeyword() { return this.keyword; }
    
    @Override
    public List<String> getArgumentList() { return this.args; }
    
    //
    // =========================================================================
    //
    
    /**
     * A constructor that allows you to make generic responses.
     * 
     * @param transactionId The matching transaction ID.
     * @param keyword The appropriate protocol keyword.
     * @param success Did the response succeed?
     */
    protected ServerResponse(int transactionId, String keyword, boolean success) {
        super(transactionId);
        
        this.request = null;
        this.keyword = keyword;
        this.args = new LinkedList<String>();
        this.addSuccess(success);
    }
    
    /**
     * Constructor.
     * 
     * @param request The request the response is intended for.
     */
    protected ServerResponse(Message request) {
    	super(request.getTransactionId());	
    	this.request = request;
        this.keyword = request.getKeyword();
    }
    
    /**
     * Constructor. Creates an argument list with OKAY/FAIL depending on the
     * success value.
     *
     * @param request The message the response is intended for.
     * @param success True if response is for success, false otherwise.
     */
    protected ServerResponse(Message request, boolean success) {      
        this(request); 
        
        // Create our argument list.
        this.args = new LinkedList<String>();
        this.addSuccess(success);
    }
    
    /**
     * Constructor. Supplied argument list is used.
     * 
     * @param request The message the response is intended for.
     * @param args A list of arguments to be sent with the response.
     */
    protected ServerResponse(Message request, boolean success, List<String> args) {
    	this(request, success);
    	this.args.addAll(args);
    }
}
